<template>

	<section>
		<!--工具条--><!--  高级查询相关-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="员工姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>
<!--    id-->
<!--    username-->
<!--    phone-->
<!--    email-->
<!--    salt-->
<!--    password-->
<!--    age-->
<!--    state-->
<!--    department_id-->
<!--    logininfo_id-->
<!--    shop_id-->
		<!--列表-->
    <el-table :data="emps" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
<!--      批量删除按钮-->
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="员工姓名" width="120" >
      </el-table-column>
      <el-table-column prop="phone" label="电话" width="100" >
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="100" >
      </el-table-column>
      <el-table-column prop="salt" label="盐" width="100" >
      </el-table-column>
      <el-table-column prop="password" label="密码" width="100" >
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="100" >
      </el-table-column>

      <el-table-column prop="state" label="状态" width="100" :formatter="formatSex" sortable>
<!--        自定义状态展示列表-->
        <template scope="scope">
          <span v-if="scope.row.state==1" style="color: green">启用</span>
          <span v-else-if="scope.row.state==0" style="color: red">禁用</span>
          <span v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column prop="password" label="密码" width="100" >
      </el-table-column>
      <el-table-column prop="department_id" label="所属部门" min-width="180" >
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
<!--      批量删除的按钮-->
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="total"
                     style="float:right;">
			</el-pagination>
		</el-col>
<!--    id-->
<!--    username-->
<!--    phone-->
<!--    email-->
<!--    salt-->
<!--    password-->
<!--    age-->
<!--    state-->
<!--    department_id-->
<!--    logininfo_id-->
<!--    shop_id-->

		<!--编辑界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px"  ref="editForm">
				<el-form-item label="用户名" prop="name">
					<el-input v-model="editForm.username" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="电话" prop="sn">
          <el-input v-model="editForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="sn">
          <el-input v-model="editForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="盐" prop="sn">
          <el-input v-model="editForm.salt" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="sn">
          <el-input v-model="editForm.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="sn">
          <el-input v-model="editForm.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="editForm.state">
            <el-radio class="radio" :label="1">启用</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="部门id" prop="sn">
          <el-input v-model="editForm.department_id" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门编号" prop="sn">
          <el-input v-model="editForm.logininfo_id" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺id" prop="sn">
          <el-input v-model="editForm.shop_id" auto-complete="off"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getDepartmentListPage, removeDepartment, batchRemoveDepartment, editDepartment, addDepartment } from '../../api/api';

	export default {
		data() {
			return {
        departmentTrees:[],
			  employees:[],
				filters: {
					keyword: ''
				},
        title:"",
				emps: [],
				total: 0,
				currentPage: 1,
        pageSize:5,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,

				//编辑界面数据
				editForm: {
          id:'',
          username:'',
          phone:'',
          email:'',
          salt:'',
          password:'',
          age:'',
          state:'',
          department_id:'',
          logininfo_id:'',
          shop_id:''
				},
			}
		},
		methods: {
      getEmployees (){
        this.$http.get("/emp").then(result=>{
          console.debug(result)
          this.employees=result.data
          console.debug(result.data)
        })
      },
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDepartments();
			},
      search(){

        let para = {
          currentPage:1,
          pageSize:this.pageSize,
          keyword: this.filters.keyword
        };
        //开启盲等
        this.listLoading = true;
        this.$http.post("/emp",para).then(res=>{
          //  数据请求成功之后关闭忙等
          this.listLoading=false;
          //  获取后端返回的数据
          console.debug(res)
          this.emps=res.data.rows;
          this.total=res.data.total
        })
      },
			//获取部门列表
			getDepartments() {
				let para = {
          currentPage: this.currentPage,
          pageSize:this.pageSize,
          keyword: this.filters.keyword
				};
				//开启盲等
				this.listLoading = true;
        this.$http.post("/emp",para).then(res=>{
        //  数据请求成功之后关闭忙等
          this.listLoading=false;
          this.emps=res.data.rows;
          this.total=res.data.total
        })
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { id: row.id };
					this.$http.delete("/emp/"+row.id).then(result=>{
					//  关闭盲灯
            this.listLoading = false;
            // 1.获取后端传递的AjaxResult对象
            result = result.data;
            // 2.判断删除是否成功
            if (result.success){
              // 删除成功
              // 1.给出成功提示
              this.$message({
                message: result.msg,
                type: 'success'
              });
              // 2.修改页码为第一页
              this.currentPage = 1;
              // 3.调用函数刷新列表
              this.getDepartments();
            }else {
              // 删除失败
              // 删除成功
              // 1.给出失败提示
              this.$message({
                message: result.msg,
                type: 'error'
              });
              // 2.修改页码为第一页
              this.currentPage = 1;
              // 3.调用函数刷新列表
              this.getDepartments();
            }
          })
				}).catch(result=>{
				//  关闭盲灯
          this.listLoading=false;
        //  给出失败提示
          this.$message({
            message: '系统繁忙，请重试！',
            type: 'error'
          });
        }).catch(()=>{

        });
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.title="编辑";
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
        if(row.dirPath){
          let arr=row.dirPath.split("/");
          let parents=[];
          for (let i = 1; i <arr.length-1 ; i++) {
            parents.push(parseInt(arr[i]))
          }
          this.editForm.department=parents;
        }
			},
			//显示新增界面
			handleAdd: function () {
        this.title="新增";
				this.editFormVisible = true;
				this.editForm = {
          id:'',
          username:'',
          phone:'',
          email:'',
          salt:'',
          password:'',
          age:'',
          state:'',
          department_id:'',
          logininfo_id:'',
          shop_id:''
				};
			},
			//编辑提交按钮
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);//表单数据拷贝
            //  格式化数据
              if(para.parent){
                para.parent={"id":para.parent[para.parent.length-1]};
              }
            //  发送axios请求
              this.$http.put("/emp",para).then(result=>{
            //  关闭盲灯
                this.editLoading = false;
                // 1.获取后端传递的AjaxResult对象
                result = result.data;
                // 2.判断提交是否成功
                if (result.success){
                  // 提交成功
                  // 1.给出成功提示
                  this.$message({
                    message: result.msg,
                    type: 'success'
                  });
                  // 2.修改页码为第一页
                  this.currentPage = 1;
                  // 3.调用函数刷新列表
                  this.getDepartments();
                }else {
                  // 1.给出失败提示
                  this.$message({
                    message: result.msg,
                    type: 'error'
                  });
                  // 2.修改页码为第一页
                  this.currentPage = 1;
                  // 3.调用函数刷新列表
                  this.getDepartments();
                }
              })
            }).catch(result=>{
              //  关闭盲灯
              this.editLoading=false;
              //  给出失败提示
              this.$message({
                message: '系统繁忙，请重试！',
                type: 'error'
              });
              })
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
			  //
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.patch("/emp",ids).then(result=>{
            result=result.data;
            if(result.success){
              // 删除成功
              // 1.给出成功提示
              this.$message({
                message: result.msg,
                type: 'success'
              });
              // 2.修改页码为第一页
              this.currentPage = 1;
              // 3.调用函数刷新列表
              this.getDepartments();
            }else {
              // 删除失败
              // 删除成功
              // 1.给出失败提示
              this.$message({
                message: result.msg,
                type: 'error'
              });
              // 2.修改页码为第一页
              this.currentPage = 1;
              // 3.调用函数刷新列表
              this.getDepartments();
            }
          }).catch(result=>{
            // 1.关闭忙等框
            this.listLoading = false;
            // 2.给出自定义失败提示！
            this.$message({
              message: '系统繁忙，请重试！',
              type: 'error'
            });
          })
				}).catch(() => {

				});
			},


		},

		mounted() {
			this.getDepartments();
			this.getEmployees();
		}
	}

</script>

<style scoped>

</style>